package com.example.taxfloatproject20240926.ui.socket;

import android.text.TextUtils;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gf on 15-3-18.
 */
public class CmdUtils {

    public static final String TAG = "socketClientTag";
    public static boolean isConnect = false;

    public static Map<String, String> parseCmd(String cmd) {
        Map<String, String> map = new HashMap<>();
        if (cmd.contains("/")) {
            String[] str = cmd.split("/");
            int count = str.length;
            if (count >= 4) {
                if ("".equals(str[0]) && "End".equals(str[count - 1])) {
                    for (int i = 0; i < count; i++) {
                        if (str[i].contains(" ")) {
                            String res[] = str[i].split(" ");
                            Log.i(TAG, "res.length==" + res.length);
                            if (res.length == 2) {
                                for (int j = 0; j < res.length; j++) {
                                    map.put(res[0], res[1]);
                                }
                            }
                            if (res.length == 3) {
                                for (int j = 0; j < res.length; j++) {
                                    map.put(res[0], res[1] + " " + res[2]);
                                }
                            }

                        }
                    }
                }
            }

        }

        return map;
    }

    public static List<String> splitMsg(String msg) {
        List<String> list = new ArrayList<>();
        if (!TextUtils.isEmpty(msg) && msg.contains("/cmd") && msg.contains("/End")) {
            String[] strs = msg.split("/cmd");
            for (String str : strs) {
                if (!TextUtils.isEmpty(str) && str.contains("/End")) {
                    list.add("/cmd" + str);
                }
            }
            return list;
        }
        return null;
    }


}
